from django.db import models
from django.core import validators
from student_front.models import Students
from DjangoUeditor.models import UEditorField

class Teachers(models.Model):
    number = models.CharField(max_length=12,primary_key=True,validators=[validators.RegexValidator(r'\d{12}',message='请输入正确格式的教工号！')])
    name = models.CharField(max_length=16,validators=[validators.MinLengthValidator(2)])
    password = models.CharField(max_length=256,validators=[validators.MinLengthValidator(2)])
    college = models.CharField(max_length=26)

    class Meta:
        db_table = 'Teachers'

class Subjects(models.Model):
    name = models.CharField(max_length=26)
    creat_time = models.DateTimeField(auto_now_add=True)
    teacher = models.ForeignKey(Teachers,on_delete=models.CASCADE)
    student = models.ForeignKey(Students,on_delete=models.CASCADE)
    exam_set = models.BooleanField(default=False)

    class Meta:
        db_table = 'Subjects'


class ExamPaper(models.Model):
    name = models.CharField(max_length=26)
    teacher = models.ForeignKey(Teachers,on_delete=models.CASCADE)
    class Meta:
        db_table = 'ExamPaper'


class Exam(models.Model):
    name = models.CharField(max_length=26)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    time_limit = models.PositiveIntegerField()
    subject = models.OneToOneField(Subjects,on_delete=models.CASCADE)
    state = models.BooleanField(default=False)
    exampaper = models.ForeignKey(ExamPaper,on_delete=models.CASCADE,null=True)
    total_score = models.IntegerField(default=0)

    class Meta:
        db_table = 'Exam'


class ExamExtension(models.Model):
    question = UEditorField(width=1000, height=300, toolbars=[['undo','redo','|','bold','italic','underline','fontfamily','fontsize','insertimage','forecolor','lineheight']], imagePath="images/", filePath="files/", upload_settings={"imageMamxSize":1204000},
             settings={}, verbose_name='题干描述', blank=True, null=True)
    type = models.CharField(max_length=16)
    answer = models.CharField(max_length=4,null=True,verbose_name='正确选项')
    value = models.IntegerField(verbose_name='分值')
    exam = models.ForeignKey(ExamPaper,on_delete=models.CASCADE)

    class Meta:
        db_table = 'ExamExtension'

class StudentsAnswer(models.Model):
    ans = UEditorField(width=1000, height=300, toolbars=[['undo','redo','|','bold','italic','underline','fontfamily','fontsize','insertimage','forecolor','lineheight']], imagePath="images/", filePath="files/", upload_settings={"imageMamxSize":1204000},
             settings={}, verbose_name='请作答', blank=True, null=True)
    total_score = models.IntegerField(default=0)
    examextension = models.ForeignKey(ExamExtension,on_delete=models.CASCADE)
    state = models.BooleanField(default=False)
    subject = models.ForeignKey(Subjects,on_delete=models.CASCADE,null=True)

    class Meta:
        db_table = 'StudentsAnswer'

